package cn.dnui_zjq823.dnuifood_16110100823.controller;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

import cn.dnui_zjq823.dnuifood_16110100823.R;
import cn.dnui_zjq823.dnuifood_16110100823.fragment.CollectionFragment;
import cn.dnui_zjq823.dnuifood_16110100823.fragment.MyFragment;
import cn.dnui_zjq823.dnuifood_16110100823.fragment.SearchFragment;
import cn.dnui_zjq823.dnuifood_16110100823.fragment.ShopFragment;

public class MainActivity extends AppCompatActivity {
    private Button btn_shop,btn_collect,btn_search,btn_mine;
    private ViewPager vp;
    private List<Fragment> listfragment;
    private FragmentPagerAdapter fragmentPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initfragment();
    }

    private void init() {
        btn_shop=findViewById(R.id.btn_shop);
        btn_collect=findViewById(R.id.btn_collect);
        btn_search=findViewById(R.id.btn_search);
        btn_mine=findViewById(R.id.btn_mine);
        vp=findViewById(R.id.viewpager);

        btn_shop.setOnClickListener(onClickListener);
        btn_collect.setOnClickListener(onClickListener);
        btn_search.setOnClickListener(onClickListener);
        btn_mine.setOnClickListener(onClickListener);
    }
    View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            resetImage();
            switch (view.getId()){
                case R.id.btn_shop:
                    selectTab(0);
                    break;
                case R.id.btn_collect:
                    selectTab(1);
                    break;
                case R.id.btn_search:
                    selectTab(2);
                    break;
                case R.id.btn_mine:
                    selectTab(3);
                    break;
            }
        }
    };

    private void resetImage() {
    }
    private void selectTab(int i){
        vp.setCurrentItem(i);
    }

    private void initfragment() {
        listfragment=new ArrayList<Fragment>();
        listfragment.add(new ShopFragment());
        listfragment.add(new CollectionFragment());
        listfragment.add(new SearchFragment());
        listfragment.add(new MyFragment());
        fragmentPagerAdapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return listfragment.get(position);
            }

            @Override
            public int getCount() {
                return listfragment.size();
            }
        };
        vp.setAdapter(fragmentPagerAdapter);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                resetImage();
                selectTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
